package com.admin.application;

import com.admin.domain.modle.Advert;
import com.admin.domain.modle.Order;
import com.admin.domain.modle.Play;
import com.admin.domain.repository.AdvertRepository;
import com.admin.infrastructure.DateUtil;
import com.admin.infrastructure.DateUtils;
import com.admin.infrastructure.RemoteDataUtil;
import com.admin.utils.OrderStatus;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;


/**
 * description
 * User: WeiDa
 * Date: 2017-4-11
 * Time: 16:45
 */
@Service
public class TaskService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TaskService.class);
    @Autowired
    protected OrderService orderService;
    @Autowired
    protected PlayService playService;

    @Transactional
    public void cancelPreOrder() throws ParseException {
        String preStatus  = OrderStatus.PRE.name();
        List<Order> list = orderService.listByStatus(preStatus);
        if(CollectionUtils.isEmpty(list)) return;
        subCancel(list);
    }


    @Transactional
    public void cancelInitOrder() throws ParseException {
        String initStatus = OrderStatus.INIT.name();
        List<Order> list = orderService.listByStatus(initStatus);
        if(CollectionUtils.isEmpty(list)) return;
        subCancel(list);
    }

    private void subCancel(List<Order> list) throws ParseException {
        LocalDateTime t2 = LocalDateTime.now();
        for (Order order :list) {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = sdf.format(order.getCreateTime());
            LocalDateTime t1 = DateUtils.parseLocalDateTime(str);
            long result = DateUtils.getMinutesBetween(t1,t2);
            if(result<15) continue;
            orderService.cancelOrder(order.getOrderId());
        }
    }
    @Transactional
    public void verifyOrder() {
        String status = OrderStatus.PAID.name();
        List<Order> list = orderService.listByStatus(status);
        if(CollectionUtils.isEmpty(list)) return;
        for (Order order :list) {
            orderService.verifyOrder(order.getOrderId());
        }
    }

    @Transactional
    public void editSaleDate(){
        List<Play>  list = playService.list2(null);
        if(CollectionUtils.isEmpty(list)) return;
        for (Play play:list){
            String saleDates = play.getSaleDate();
            List<String> saleDateList = JSON.parseArray(saleDates,String.class);
            if(CollectionUtils.isEmpty(saleDateList)){

                for (int i = 0; i < 6; i++) {
                    saleDateList.add(DateUtil.getStrDate(i+""));
                }
                playService.updateSaleDateById(play.getPlayId(),JSON.toJSONString(saleDateList));
            }else{
                saleDateList.remove(DateUtil.getBackStrDate("1"));
            }
        }
    }
}
